import VueRouter from "vue-router";
import Vue from 'vue';
import Login from "../components/Login.vue";
import Register from "../components/Register.vue";
import Index from "../view/Index.vue";
import Sort from "../view/Sort.vue";
import Find from "../view/Find.vue";
import Myjd from "../view/Myjd.vue";
import Book from "../components/Sort/Book.vue";
import Computer from "../components/Sort/Computer.vue";
import Electrical from "../components/Sort/Electrical.vue";
import Hot from "../components/Sort/Hot.vue";
import Infant from "../components/Sort/Infant.vue";
import Manwear from "../components/Sort/Manwear.vue";
import Phone from "../components/Sort/Phone.vue";
import Textiles from "../components/Sort/Textiles.vue";
import Underwear from "../components/Sort/Underwear.vue";
import Womenwear from "../components/Sort/Womenwear.vue";
import Shopcar from "../view/Shopcar.vue";
import Alls from "../components/Find/Alls.vue";
import Cars from "../components/Find/Cars.vue";
import Jiadian from "../components/Find/Jiadian.vue";
import Jiaju from "../components/Find/Jiaju.vue";
import Numerical from "../components/Find/Numerical.vue";
import Search from "../components/Search.vue";
import Search_list from "../components/Searchs/Search_list.vue";
import Search_index from "../components/Searchs/Search_index.vue"
import Detail from "../components/particulars/Detail.vue";
import Peisong from "../components/particulars/Peisong.vue";
import Jiesuan from "../components/Shopcar/Jiesuan.vue"
const router = new VueRouter({
  routes: [
    {
      path: "/",
      redirect: "/Index",
    },
    {
      path: "/Index",
      name:'Index',
      component: Index,
      // 配置路由元信息
      meta: {
        isRequired: false,
      }
    },
    {
      path: "/Search",
      name:'Search',
      component: Search,
      children:[
        // 初次进入默认进入到搜索的首页
        {
          path: "/",
          name:'index',
          component: Search_index,
         },
        {
          path: "/list",
          name:'list',
          component: Search_list,
         },
      ],
      meta: {
        isRequired: false,
      },
    },
    {
      path: "/detail",
      name:'Detail',
      component: Detail,
    },
    {
      path: "/jiesuan",
      name:'jiesuan',
      component: Jiesuan,
    },
    {
      path:'/Peisong',
      name:'Peisong',
      component:Peisong,
    },

    {
      path: "/Sort",
      name:'sort',
      component: Sort,
      redirect:'/Hot',
      children:[
        {
          path:'Book',
          // name:'book',
          component:Book,
        },
        {
          path:'Computer',
          // name:'computer',
          component:Computer,
        },
        {
          path:'Electrical',
          // name:'electrical',
          component:Electrical
        },
        {
          path:'Hot',
          // name:'hot',
          component:Hot,
          meta: {
            isRequired: false,
          },
        },
        {
          path:'Infant',
          // name:'infant',
          component:Infant
        },
        {
          path:'Manwear',
          // name:'manwear',
          component:Manwear
        },
        {
          path:'Phone',
          // name:'phone',
          component:Phone
        },
        {
          path:'Textiles',
          // name:'textiles',
          component:Textiles
        },
        {
          path:'Underwear',
          // name:'underwear',
          component:Underwear
        },
        {
          path:'Womenwear',
          // name:'womenwear',
          component:Womenwear
        },
      ]
    },
    {
      path: "/Find",
      name:'find',
      component: Find,
      redirect:'/Alls',
      children:[
        {
          path:'Alls',
          // name:'alls',
          component:Alls,
          meta: {
            isRequired: false,
          },
        },
        {
          path:'Cars',
          // name:'cars',
          component:Cars
        },
        {
          path:'Jiadian',
          // name:'jiadian',
          component:Jiadian
        },
        {
          path:'Jiaju',
          // name:'jiaju',
          component:Jiaju
        },
        {
          path:'Numerical',
          // name:'numerical',
          component:Numerical
        },
      ]
    },
    {
      path: "/Shopcar",
      name:'shopcar',
      component: Shopcar,
      meta: {
        isRequired: true,
      },
    },
    {
      path: "/Myjd",
      name:'myjd',
      component: Myjd,
      meta: {
        isRequired: true,
      },
    },
    {
      path: "/Login",
      name:'login',
      component: Login,
    },
    {
      path: "/Register",
      name:'register',
      component: Register,
    },
  ],
});

  router.beforeEach((to,from,next)=>{
    if (to.meta.isRequired) {  //如果该路由需要验证元信息
      if (sessionStorage.getItem('token')) { //token存在就访问该路由
        next()
      }else{//不存在就跳往登录界面
        alert("您还未登录,请先登录")
        next({path: '/login'})
      }
    }else{
      next()//不需要验证直接放行跳转
    }
  })
//暴露router
export default router;
